package com.lujianfei.tiananyungu.utils

/**
 * gps转百度坐标系
 * @author ardo
 *
 * WGS-84：是国际标准，GPS坐标（Google Earth使用、或者GPS模块）
 * GCJ-02：中国坐标偏移标准，Google
 * Map、高德、腾讯使用 BD-09：百度坐标偏移标准，Baidu Map使用
 *
 * pi: 圆周率。
 * a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
 * ee: 椭球的偏心率。
 * x_pi: 圆周率转换量。
 * transformLat(lat, lon): 转换方法，比较复杂，不必深究了。输入：横纵坐标，输出：转换后的横坐标。
 * transformLon(lat, lon): 转换方法，同样复杂，自行脑补吧。输入：横纵坐标，输出：转换后的纵坐标。
 * wgs2gcj(lat, lon): WGS坐标转换为GCJ坐标。
 * gcj2bd(lat, lon): GCJ坐标转换为百度坐标。
 *
 * 出现差错的 把 x的 y位置调一下
 */
object GpsToBaidu {
    private const val  pi = 3.14159265358979324
    private const val  a = 6378245.0
    private const val  ee = 0.00669342162296594323
    private const val x_pi = 3.14159265358979324 * 3000.0 / 180.0

    fun wgs2bd(lat: Double, lon: Double): DoubleArray {
        val wgs2gcj = wgs2gcj(lat, lon)
        return gcj2bd(wgs2gcj[0], wgs2gcj[1])
    }

    fun distance(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
        val R = 6371 // 地球半径，单位为公里
        val dLat = Math.toRadians(lat2 - lat1)
        val dLon = Math.toRadians(lon2 - lon1)
        val a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLon / 2) * Math.sin(dLon / 2)
        val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
        return R * c
    }

    private fun gcj2bd(lat: Double, lon: Double): DoubleArray {
        val z = Math.sqrt(lon * lon + lat * lat) + 0.00002 * Math.sin(lat * x_pi)
        val theta = Math.atan2(lat, lon) + 0.000003 * Math.cos(lon * x_pi)
        val bd_lon = z * Math.cos(theta) + 0.0065
        val bd_lat = z * Math.sin(theta) + 0.006
        return doubleArrayOf(bd_lat, bd_lon)
    }

    private fun wgs2gcj(lat: Double, lon: Double): DoubleArray {
        var dLat = transformLat(lon - 105.0, lat - 35.0)
        var dLon = transformLon(lon - 105.0, lat - 35.0)
        val radLat = lat / 180.0 * pi
        var magic = Math.sin(radLat)
        magic = 1 - ee * magic * magic
        val sqrtMagic = Math.sqrt(magic)
        dLat =
            dLat * 180.0 / (a * (1 - ee) / (magic * sqrtMagic) * pi)
        dLon =
            dLon * 180.0 / (a / sqrtMagic * Math.cos(radLat) * pi)
        val mgLat = lat + dLat
        val mgLon = lon + dLon
        return doubleArrayOf(mgLat, mgLon)
    }

    private fun transformLat(lat: Double, lon: Double): Double {
        var ret =
            -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(
                Math.abs(lat)
            )
        ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0
        ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0
        ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0
        return ret
    }

    private fun transformLon(lat: Double, lon: Double): Double {
        var ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(
            Math.abs(lat)
        )
        ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0
        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0
        ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0
        return ret
    }
}